package cn.cdutetc.library.controller;

import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.dto.FeeVO;
import cn.cdutetc.library.entity.dto.FeeRecordVO;
import cn.cdutetc.library.entity.dto.FeeStatisticsVO;
import cn.cdutetc.library.service.FeeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 费用管理控制器
 */
@RestController
@RequestMapping("/api/fees")
@Tag(name = "费用管理", description = "费用计算相关接口")
public class FeeController {

    @Autowired
    private FeeService feeService;

    @GetMapping("/overdue")
    @Operation(summary = "计算逾期罚款", description = "根据用户ID计算逾期罚款，已存在的记录不会重复添加")
    public ApiResponse<FeeVO> calculateOverdueFee(
            @RequestParam @Parameter(description = "用户ID") Integer userId) {
        FeeVO feeVO = feeService.calculateOverdueFee(userId);
        return ApiResponse.success(feeVO);
    }

    @GetMapping("/damage")
    @Operation(summary = "计算损坏赔偿", description = "根据用户ID、图书ID和损坏比例计算损坏赔偿费用，已存在的记录不会重复添加")
    public ApiResponse<FeeVO> calculateDamageFee(
            @RequestParam @Parameter(description = "用户ID") Integer userId,
            @RequestParam @Parameter(description = "图书ID") Integer bookId,
            @RequestParam @Parameter(description = "损坏比例（0-2之间）") Double damageRatio) {
        FeeVO feeVO = feeService.calculateDamageFee(userId, bookId, damageRatio);
        return ApiResponse.success(feeVO);
    }

    @GetMapping("/lost")
    @Operation(summary = "计算丢失赔偿", description = "根据用户ID、图书ID和丢失比例计算丢失赔偿费用，已存在的记录不会重复添加")
    public ApiResponse<FeeVO> calculateLostFee(
            @RequestParam @Parameter(description = "用户ID") Integer userId,
            @RequestParam @Parameter(description = "图书ID") Integer bookId,
            @RequestParam(defaultValue = "1.0") @Parameter(description = "丢失比例（0-3之间，默认为1）") Double lostRatio) {
        FeeVO feeVO = feeService.calculateLostFee(userId, bookId, lostRatio);
        return ApiResponse.success(feeVO);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户费用记录", description = "分页获取指定用户的费用记录")
    public ApiResponse<IPage<FeeRecordVO>> getUserFeeRecords(
            @PathVariable @Parameter(description = "用户ID") Integer userId,
            @RequestParam(defaultValue = "1") @Parameter(description = "页码") Integer page,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页数量") Integer size,
            @RequestParam(required = false) @Parameter(description = "费用类型(overdue_fee/damage_fee/loss_fee)") String feeType,
            @RequestParam(required = false) @Parameter(description = "状态(paid/unpaid)") String status) {
        IPage<FeeRecordVO> result = feeService.getUserFeeRecords(userId, page, size, feeType, status);
        return ApiResponse.success(result);
    }

    @GetMapping("/user/{userId}/stats")
    @Operation(summary = "获取用户费用统计", description = "获取指定用户的费用统计数据")
    public ApiResponse<FeeStatisticsVO> getUserFeeStats(@PathVariable @Parameter(description = "用户ID") Integer userId) {
        FeeStatisticsVO statistics = feeService.getUserFeeStatistics(userId);
        return ApiResponse.success(statistics);
    }

    @PostMapping("/{feeId}/pay")
    @Operation(summary = "支付费用", description = "支付指定的费用")
    public ApiResponse<Boolean> payFee(
            @PathVariable @Parameter(description = "费用ID") Integer feeId,
            @RequestParam @Parameter(description = "支付方式(alipay/wechat/cash)") String payMethod) {
        boolean result = feeService.payFee(feeId, payMethod);
        return ApiResponse.success(result);
    }
}
    